/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpImportMaster;
import com.jims.master.modules.csm.service.ExpImportMasterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 入库主记录Controller
 * @author zhuq
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expImportMaster")
public class ExpImportMasterController extends BaseController {

	@Autowired
	private ExpImportMasterService expImportMasterService;

	@ModelAttribute
	public ExpImportMaster get(@RequestParam(required=false) String id) {
		ExpImportMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expImportMasterService.get(id);
		}
		if (entity == null){
			entity = new ExpImportMaster();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpImportMaster expImportMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpImportMaster> page = expImportMasterService.findPage(new Page<ExpImportMaster>(request, response), expImportMaster);
		model.addAttribute("page", page);
		return "modules/csm/expImportMasterIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpImportMaster expImportMaster, Model model) {
		model.addAttribute("expImportMaster", expImportMaster);
		return "modules/csm/expImportMasterForm";
	}

	/**
	 * 入库单据查询
	 * @param expImportMaster
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"query", ""})
	public String listque(ExpImportMaster expImportMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpImportMaster> page = expImportMasterService.findImportList(new Page<ExpImportMaster>(request, response), expImportMaster);
		Date date = expImportMaster.getCreateDate();
		Date date1 = expImportMaster.getUpdateDate();

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (date != null) {
			expImportMaster.setCreateDateStr(dateformat.format(date));
		}
		if (date1 != null) {
			expImportMaster.setUpdateDateStr(dateformat.format(date1));
		}
		model.addAttribute("expImportMaster",expImportMaster);
		model.addAttribute("page", page);
		return "modules/csm/expImportQueryIndex ";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpImportMaster expImportMaster, Model model) {
	    StringData data=new StringData();
		int i=expImportMasterService.save(expImportMaster);
        if(i>0){
            data.setCode("success");
            data.setData("保存入库成功");
        }else{
            data.setCode("error");
            data.setData("保存入库失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpImportMaster expImportMaster) {
	    StringData data=new StringData();
		int i=expImportMasterService.delete(expImportMaster);
        if(i>0){
            data.setCode("success");
            data.setData("删除入库成功");
        }else{
            data.setCode("error");
            data.setData("删除入库失败");
        }
		return data;
	}

}